package com.devshephard.juchm.reentrantlock;

import java.util.concurrent.locks.ReentrantLock;

import static java.lang.Thread.sleep;

public class ReentrantLockTryLockDemo {

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(() -> {
            System.out.println(("启动..."));  //可以使用trylock方法尝试获取锁,成功与否返回布尔值
            if (!lock.tryLock()) {
                System.out.println(("获取立刻失败，返回"));
                return;
            }
            try {
                System.out.println(("获得了锁"));
            } finally {
                lock.unlock();
            }
        }, "t1");
        lock.lock();
        System.out.println(("获得了锁"));
        t1.start();
        try {
            sleep(2);
        } finally {
            lock.unlock();
        }
    }

}
